package com.RLFZ.tengxunapi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.RLFZ.entity.ChinaTotal;
import com.RLFZ.entity.NocvData;
import com.RLFZ.service.ChinaTotalService;
import com.RLFZ.service.IndexService;
import com.RLFZ.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import redis.clients.jedis.Jedis;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author yao
 * @since 2022/8/8
 */
@EnableScheduling
@Configuration
public class ChinaTotalScheduleTask {

        @Autowired
        ChinaTotalService chinaTotalService;
        @Autowired
        private IndexService indexService;
        @Autowired
        private UserService userService;

        public  static ChinaTotalScheduleTask chinaTotalScheduleTask ;

        @PostConstruct
        public  void  init(){
            chinaTotalScheduleTask = this;
            chinaTotalScheduleTask.chinaTotalService = this.chinaTotalService;
            chinaTotalScheduleTask.userService = this.userService;
            chinaTotalScheduleTask.indexService = this.indexService;
        }

    @Scheduled(fixedDelay  = 10000000)   //1000s 执行一次
//    @Scheduled(cron = "0 0 8,9,10,11,12,13,18,20 * * ?")
    public void updateChinaTotalDB() throws IOException, ParseException {
        HttpUtils httpUtils = new HttpUtils();
        String string = httpUtils.getData();
//        System.out.println("原始数据:"+string);
        // 1.所有json格式数据
        JSONObject jsonObject = JSONObject.parseObject(string);
        Object data = jsonObject.get("data");
//        System.out.println(data.toString());
//        System.out.println("====================================");
        //  2. date
        JSONObject jsonObjectData = JSONObject.parseObject(data.toString());
//        ChinaTotal chinaTotal = (ChinaTotal) jsonObject1.get("chinaTotal");
//        System.out.println(chinaTotal);
        Object chinaTotal = jsonObjectData.get("chinaTotal");
        Object lastUpdateTime = jsonObjectData.get("overseaLastUpdateTime");
//        System.out.println("chinaTotal" + chinaTotal);
        //3. total 全中国整体疫情数据
        JSONObject jsonObjectTotal = JSONObject.parseObject(chinaTotal.toString());
        Object total = jsonObjectTotal.get("total");
//        System.out.println("total:"+total);
        //4. 全国数据total
        JSONObject totalData = JSONObject.parseObject(total.toString());
        Object confirm = totalData.get("confirm");
        Object input = totalData.get("input");
        Object severe = totalData.get("severe");
        Object heal = totalData.get("heal");
        Object dead = totalData.get("dead");
        Object suspect = totalData.get("suspect");
        // 5 位程序实体赋值
        ChinaTotal dataEntry = new ChinaTotal();
        dataEntry.setConfirm((Integer)confirm);
        dataEntry.setInput((Integer)input);
        dataEntry.setSevere((Integer)severe);
        dataEntry.setHeal((Integer)heal);
        dataEntry.setDead((Integer)dead);
        dataEntry.setSuspect((Integer)suspect);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String s1 = String.valueOf(lastUpdateTime);
        SimpleDateFormat simpleDateFormat1 = simpleDateFormat;
        System.out.println(simpleDateFormat+"----------------");
        dataEntry.setUpdateTime(simpleDateFormat.parse(String.valueOf(lastUpdateTime)));
        System.out.println("--------------------"+lastUpdateTime);
        //6 插入数据库
        Date date = new Date();
        System.out.println(data+"------------");
//        chinaTotalScheduleTask.chinaTotalService.save(dataEntry);
        chinaTotalService.insertt((Integer)confirm,(Integer)input,(Integer)severe,(Integer)heal,(Integer)dead,(Integer)suspect,String.valueOf(lastUpdateTime));
        //7 删除缓存     一定要

        // 3.世界各个国家及地区的所有数据
        Object areaTree = jsonObjectData.get("areaTree");
//        System.out.println("areaTree:"+areaTree);

        JSONArray areaTree1 = jsonObjectData.getJSONArray("areaTree");
        Object[] objects = areaTree1.toArray();
// 所有国家的名字
        for (int i = 0; i < objects.length; i++) {
            JSONObject jsonObject1 = JSONObject.parseObject(objects[i].toString());
            Object name = jsonObject1.get("name");
            //System.out.println(name);
        }

// 数组中第三个为中国省份数据
        JSONObject jsonObject1 = JSONObject.parseObject(objects[2].toString());
        JSONArray children1 = jsonObject1.getJSONArray("children");
        Object[] objects1 = children1.toArray();

// 遍历中国地区的数据
        List<NocvData> list = new ArrayList<>();
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        for (int i = 0; i < objects1.length; i++) {
            NocvData nocvData = new NocvData();
            JSONObject jsonObject2 = JSONObject.parseObject(objects1[i].toString());
            Object name = jsonObject2.get("name");//省份名称
            Object total2 = jsonObject2.get("total");
            JSONObject totalJson = JSONObject.parseObject(total2.toString());
            Object confirm2 = totalJson.get("confirm");//quzhen数量
//            System.out.println(name+"："+confirm2);
            //获取省份更新的时间
            Object extData = jsonObject2.get("extData");
            JSONObject extDataJson = JSONObject.parseObject(extData.toString());
            Object lastUpdateTime1 = extDataJson.get("lastUpdateTime");
            //封装数据
            nocvData.setName(name.toString());
            nocvData.setValue(Integer.parseInt(confirm2.toString()));
            String s = String.valueOf(lastUpdateTime1);
            if (lastUpdateTime1 == null){

                nocvData.setUpdateTime(new Date());

            }else {

                nocvData.setUpdateTime(format2.parse(s));
            }

            list.add(nocvData);
        }
        // 插入数据库各个省份的数据
        chinaTotalScheduleTask.indexService.saveBatch(list);
        Jedis jedis = new Jedis("127.0.0.1");
//        jedis.auth("zh2019210256");
        String ping="";
        try{
            jedis.auth("zh2019210256");
            ping = jedis.ping();
        }catch(Exception e){
            ping = "";
        }
        if("PONG".equals(ping)){
            jedis.flushDB();//只删除程序对应的键
            //执行了 删除redis缓存命令
            System.out.println("running  redis  flushDB!!");
        }
    }
}
